var gulp = require('gulp');
var srcFolder = 'src/';
var distFolder = 'dist/';
var gls = require('gulp-live-server');
/* Mixed */
var ext_replace = require('gulp-ext-replace');
/* CSS */
var postcss = require('gulp-postcss');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('autoprefixer');
var precss = require('precss');
var cssnano = require('cssnano');

/* JS & TS */
var jsuglify = require('gulp-uglify');
var typescript = require('gulp-typescript');

/* Images */
var imagemin = require('gulp-imagemin');

var tsProject = typescript.createProject('tsconfig.json');

gulp.task('build-css', function () {
    return gulp.src(srcFolder + 'scss/*.scss')
            .pipe(sourcemaps.init())
            .pipe(postcss([precss, autoprefixer, cssnano]))
            .pipe(sourcemaps.write())
            .pipe(ext_replace('.css'))
            .pipe(gulp.dest(distFolder + 'css/'));
});

gulp.task('build-ts', function () {
    return gulp.src(srcFolder + '**/*.ts')
            .pipe(sourcemaps.init())
            .pipe(typescript(tsProject))
            .pipe(sourcemaps.write())
            //.pipe(jsuglify())
            .pipe(gulp.dest(distFolder));
});

gulp.task('build-img', function () {
    return gulp.src(srcFolder + 'images/**/*')
            .pipe(imagemin({
                progressive: true
            }))
            .pipe(gulp.dest(distFolder + 'images/'));
});

gulp.task('build-html', function () {
    return gulp.src(srcFolder + '**/*.html')
            .pipe(gulp.dest(distFolder));
});
gulp.task('serve',function(){
    var server = gls.static();
    server.start();
});
gulp.task('default',function(){});
gulp.task('watch', function () {
    gulp.watch(srcFolder + '**/*.ts', ['build-ts']);
    gulp.watch(srcFolder + 'scss/**/*.scss', ['build-css']);
    gulp.watch(srcFolder + 'images/*', ['build-img']);
});
gulp.task('build', ['build-ts', 'build-css','build-html']);
gulp.task('run', ['build-ts', 'build-css','build-html','serve']);
